package org.yiqixue.accounts.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yiqixue.accounts.entity.Transaction;
import org.yiqixue.accounts.dto.TransactionDTO;
import org.yiqixue.accounts.service.TransactionService;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/transactions")
public class TransactionController {

    @Autowired
    private TransactionService transactionService;

    // 创建或更新交易
    @PostMapping
    public Transaction createOrUpdateTransaction(@RequestBody Transaction transaction) {
        return transactionService.saveTransaction(transaction);
    }

    // 获取所有交易
    @GetMapping
    public List<Transaction> getAllTransactions() {
        return transactionService.getAllTransactions();
    }

    // 根据 ID 获取交易
    @GetMapping("/{id}")
    public Optional<Transaction> getTransactionById(@PathVariable Long id) {
        return transactionService.getTransactionById(id);
    }

    // 根据 ID 删除交易
    @DeleteMapping("/{id}")
    public void deleteTransaction(@PathVariable Long id) {
        transactionService.deleteTransaction(id);
    }

    // 根据手机号和交易类型查询当日交易
    @GetMapping("/byPhoneAndType")
    public List<TransactionDTO> getTransactionsByPhoneAndType(
            @RequestParam String phoneNumber,  // 客户的手机号
            @RequestParam String type          // 交易类型（存款、取款、转账、付款）
    ) {
        return transactionService.getTransactionsByPhoneAndType(phoneNumber, type);
    }
}
